package C8_16_ReplaceSubclassWithFields;

import org.junit.Test;

/**
 * @author xufy
 * @version 1.0
 * @description 以字段取代子类
 **/
public class ReplaceSubclassWithFields {

    /**
     * 重构前
     */
    @Test
    public void test1(){
        Person person=new Male();
        System.out.println("重构前："+person.isMale()+" , "+person.getCode());
    }
    /**
     * 重构后
     */
    @Test
    public void test2(){
        Person2 kent=Person2.createMale();
        System.out.println("重构后："+kent.getCode());
    }

}
abstract  class Person{
    abstract boolean isMale();
    abstract  char getCode();
}
class Male extends  Person{
    @Override
    boolean isMale(){
        return true;
    }

    @Override
    char getCode() {
        return 'M';
    }
}
class Female extends  Person{
    @Override
    boolean isMale(){
        return false;
    }

    @Override
    char getCode() {
        return 'F';
    }
}

/**
 * 重构后
 */
class Person2{

    private  boolean _isMale;
    private  char _code;
    public Person2(boolean _isMale,char code){
        _isMale=_isMale;
        _code=code;
    }

    static Person2 createMale(){
        return new Male2();
//        return new Person2(true,'M');
    }
    static Person2 createFemale(){
        return new Female2();
    }

    public boolean isMale() {
        return _isMale;
    }

    public void setMale(boolean _isMale) {
        this._isMale = _isMale;
    }

    public char getCode() {
        return _code;
    }

    public void setCode(char _code) {
        this._code = _code;
    }
}
class Male2 extends Person2{
    Male2(){
        super(true,'M');
    }
}
class Female2 extends Person2{
    Female2(){
        super(false,'F');
    }
}